// 事件管理模块
var express = require('express');
const moment = require('moment');
var router = express.Router();
const { debug } = require('../config');
const { getAll, getOne, exec } = require('../db/index');

/* 前台获取事件列表页 */
router.get('/font', async function (req, res, next) {
  let { page = 1, size = 4 } = req.query;
  let sql = `select count(*) as total from event  `;
  const obj = await getOne(sql); //total
  console.log(obj);
  let offset = (page - 1) * size;
  sql = `select event_type,event_level,event_addr,event_time,event_imgurl,event_descri
  from event where event_status =2 limit ${offset},${size}`;
  console.log(sql);
  try {
    const data = await getAll(sql);
    res.send({
      code: 0,
      message: '获取事件成功',
      total: obj.total,
      result: data,
    });
  } catch (err) {
    res.send({
      code: 500,
      message: '获取事件失败',
      result: debug ? '' : err,
    });
  }
});

/* 前台获取事件列表页 */
router.get('/middle', async function (req, res, next) {
  let { page = 1, size = 4 } = req.query;
  console.log(page, size);
  let offset = (page - 1) * size;
  let sql = `select * from event where event_status !=1 limit ${offset},${size}`;
  try {
    const data = await getAll(sql);
    res.send({
      code: 0,
      message: '获取事件成功',
      result: data,
    });
  } catch (err) {
    res.send({
      code: 500,
      message: '获取事件失败',
      result: debug ? '' : err,
    });
  }
});

// 获取事件总数
router.get('/total', async function (req, res, next) {
  let { status } = req.query;
  if (status != 0 && status != 1 && status != 2) {
    return res.send({
      code: 500,
      message: '获取事件总数失败',
      result: debug ? '' : err,
    });
  }
  let sql = `select count(*) as total from event where event_status =${status}`;
  try {
    const total = await getOne(sql);
    res.send({
      code: 0,
      message: '获取事件总数成功',
      result: total,
    });
  } catch (err) {
    res.send({
      code: 500,
      message: '获取事件总数失败',
      result: debug ? '' : err,
    });
  }
});

// 添加事件，也就是报告路况

router.post('/', async function (req, res) {
  let { type, address, imgUrl, time, description, sign } = req.body;
  let sql = `insert into event(event_type,event_addr,event_time,event_imgurl,event_descri,event_signs) 
  values('${type}','${address}','${time}','${imgUrl}','${description}','${sign}')`;
  console.log(sql);
  try {
    const data = await exec(sql);

    res.send({
      code: 0,
      message: '添加事件成功',
      result: '',
    });
  } catch (err) {
    res.send({
      code: 500,
      message: '添加事件失败',
      result: debug ? '' : err,
    });
  }
});

// 改变事件状态，也就是确认事件以及删除事件,以及更新事件
router.put('/:id', async function (req, res) {
  let { id } = req.params;
  let { status, edata, fids } = req.body;
  console.log(edata);
  if (status != 0 && status != 1 && status != 2) {
    return res.send({
      code: 500,
      message: '更新事件状态失败',
      result: debug ? '' : err,
    });
  }
  let sql = `update event set event_status=${status} where event_id=${id}`;
  if (edata && fids) {
    let { type, level, addr, time, img, content, radius, sign } = edata;
    time = moment(time).format('YYYY-MM-DD h:mm:ss');
    console.log(time);
    sql = `update event set 
      event_status=${status},
      event_type='${type}',
      event_level='${level}',
      event_addr='${addr}',
      event_time='${time}',
      event_imgurl='${img}',
      event_descri='${content}',
      event_radius=${radius},
      event_signs='${sign}',
      event_fid='${fids}' 
      where event_id=${id}`;
  }
  try {
    console.log(sql);
    const data = await exec(sql);
    console.log(data);
    res.send({
      code: 0,
      message: '更新事件状态成功',
      result: { id },
    });
  } catch (err) {
    res.send({
      code: 500,
      message: '更新事件状态失败',
      result: debug ? '' : err,
    });
  }
});

router.put('/fid/:id', async function (req, res) {
  let { id } = req.params;
  let { fid } = req.body;
  let sql = `update event set event_fid=${fid} where event_id=${id}`;
  try {
    const data = await exec(sql);
    res.send({
      code: 0,
      message: '与mapgis绑定成功',
      result: '',
    });
  } catch (err) {
    res.send({
      code: 500,
      message: '与mapgis绑定失败',
      result: debug ? '' : err,
    });
  }
});
module.exports = router;
